package cn.tedu.user.management.controller;

import cn.tedu.rtms.commons.pojo.vo.PageData;
import cn.tedu.rtms.commons.web.JsonResult;
import cn.tedu.user.management.pojo.param.JobAddNewParam;
import cn.tedu.user.management.pojo.param.JobUpdateInfoParam;
import cn.tedu.user.management.pojo.param.TargetJobParam;
import cn.tedu.user.management.pojo.vo.JobEnableListItemVO;
import cn.tedu.user.management.pojo.vo.JobListItemVO;
import cn.tedu.user.management.pojo.vo.JobStandardVO;
import cn.tedu.user.management.service.IJobService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理职位相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/jobs")
@Validated
@Api(tags = "03. 职位管理模块")
public class JobController {

    @Autowired
    private IJobService jobService;

    public JobController() {
        log.debug("创建控制器类对象：JobController");
    }

    // http://localhost:9250/jobs/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/jobs/add-new')")
    @ApiOperation("新增职位")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> reg(@Valid JobAddNewParam jobAddNewParam) {
        log.debug("开始处理【添加职位】的请求，参数：{}", jobAddNewParam);
        jobService.addNew(jobAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9250/jobs/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/jobs/delete')")
    @ApiOperation("根据部门职位表ID删除职位")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门职位表ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据部门职位表ID删除职位】的请求，参数：{}", id);
        jobService.delete(id);
        return JsonResult.ok();
    }


    // http://localhost:9250/jobs
    @GetMapping("")
    @ApiOperation("查询职位列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult<PageData<JobListItemVO>> list(Integer page) {
        log.debug("开始处理【查询职位列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<JobListItemVO> pageData = jobService.list(pageNum);
        return JsonResult.ok(pageData);
    }

    // http://localhost:9250/jobs/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/jobs/update')")
    @ApiOperation("修改职位信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的职位ID值！") Long id,
                                           @Valid JobUpdateInfoParam jobUpdateInfoParam) {
        log.debug("开始处理【修改职位信息】的请求，ID：{}，新数据：{}", id, jobUpdateInfoParam);
        jobService.updateInfoById(id, jobUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9250/jobs/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询职位信息")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "职位ID", required = true, dataType = "long")
    })
    public JsonResult<JobStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的职位ID值！") Long id) {
        log.debug("开始处理【根据ID查询职位信息】的请求，参数：{}", id);
        JobStandardVO queryResult = jobService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9250/jobs/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/jobs/enable')")
    @ApiOperation("启用职位")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门职位ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用职位】的请求，参数：{}", id);
        jobService.setEnable(id);
        return JsonResult.ok();
    }
    
    // http://localhost:9250/jobs/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/jobs/disable')")
    @ApiOperation("禁用职位")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门职位ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用职位】的请求，参数：{}", id);
        jobService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9250/jobs/enableList
    @GetMapping("/enableList")
    @ApiOperation("查询招聘职位列表")
    @ApiOperationSupport(order = 430)
    public JsonResult<List<JobEnableListItemVO>> enableList(@RequestParam Long[] sectionId) {
        log.debug("开始处理【查询招聘职位列表】的请求");
        List<JobEnableListItemVO> enableList = jobService.enableList(sectionId[sectionId.length-1]);
        return JsonResult.ok(enableList);
    }

    // http://localhost:9250/jobs/targetList
    @PostMapping("/targetList")
    @ApiOperation("根据关键字查询职位列表")
    @ApiOperationSupport(order = 440)
    public JsonResult<List<JobListItemVO>> list(TargetJobParam targetJobParam) {
        log.debug("开始处理【查询目标职位列表】的请求，目标信息：{}", targetJobParam);
        List<JobListItemVO> list = jobService.selectByKeywords(targetJobParam.getKeywords());
        return JsonResult.ok(list);
    }

}